import lib.problem as problem
import unittest

DAYS_IN_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

MONDAY    = 0
TUESDAY   = 1
WEDNESDAY = 2 
THURSDAY  = 3
FRIDAY    = 4
SATURDAY  = 5
SUNDAY    = 6

class Problem019(problem.Problem):
	def __init__(self):
		problem.Problem.__init__(self, 19)
		
	def isLeapYear(self, year):
		return (year % 4 == 0) and \
	           (not (year % 100 == 0) or (year % 400 == 0))
				
		
	def getDaysInMonth(self, month, year):
		
		days = DAYS_IN_MONTH[month]
		
		if month == 1 and self.isLeapYear(year):
			days += 1
			
		return days
				
	
	def calculateNumberOfSundays(self, fromYear, toYear, dayOfWeek, firstDayOfWeek):
		
		day = firstDayOfWeek
		counter = 0
		
		for year in range(fromYear, toYear + 1):
			for month in range(12):
				if day % 7 == dayOfWeek:
					counter += 1
					
				day += self.getDaysInMonth(month, year)
			
		return counter
		
		
	def solve(self):
		return self.calculateNumberOfSundays(1901, 2000, SUNDAY, TUESDAY)
	
	
	
class Problem019Tester(unittest.TestCase):
	
	def setUp(self):
		self.p019 = Problem019()
		
	def testIsLeapYear1804(self):
		self.assertTrue(self.p019.isLeapYear(1804))
		
	def testIsLeapYear1999(self):
		self.assertFalse(self.p019.isLeapYear(1999))
		
	def testIsLeapYear1000(self):
		self.assertFalse(self.p019.isLeapYear(1000))
		
	def testIsLeapYear1600(self):
		self.assertTrue(self.p019.isLeapYear(1600))
		
	def testGetDaysInMonthFev1804(self):
		days = self.p019.getDaysInMonth(1, 1804)
		self.assertEquals(29, days)
		
	def testGetDaysInMonthFev1900(self):
		days = self.p019.getDaysInMonth(1, 1900)
		self.assertEquals(28, days)
		
	def testCalculateNumberOfSundays2009(self):
		self.assertEquals(3, self.p019.calculateNumberOfSundays(2009, 2009, SUNDAY, THURSDAY))
	
	def testCalculateNumberOfSundays1901_2000(self):
		self.assertEquals(171, self.p019.calculateNumberOfSundays(1901, 2000, SUNDAY, TUESDAY))
		
	
	def testSolve(self):
		self.assertEquals(171, self.p019.solve())
